{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import reader"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 读取数据并打印长度及前100位数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "929589\n",
      "[9970, 9971, 9972, 9974, 9975, 9976, 9980, 9981, 9982, 9983, 9984, 9986, 9987, 9988, 9989, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, 2, 9256, 1, 3, 72, 393, 33, 2133, 0, 146, 19, 6, 9207, 276, 407, 3, 2, 23, 1, 13, 141, 4, 1, 5465, 0, 3081, 1596, 96, 2, 7682, 1, 3, 72, 393, 8, 337, 141, 4, 2477, 657, 2170, 955, 24, 521, 6, 9207, 276, 4, 39, 303, 438, 3684, 2, 6, 942, 4, 3150, 496, 263, 5, 138, 6092, 4241, 6036, 30, 988, 6, 241, 760, 4, 1015, 2786, 211, 6, 96, 4]\n"
     ]
    }
   ],
   "source": [
    "DATA_PATH = \"../../datasets/PTB_data\"\n",
    "train_data, valid_data, test_data, _ = reader.ptb_raw_data(DATA_PATH)\n",
    "print len(train_data)\n",
    "print train_data[:100]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 将训练数据组织成batch大小为4、截断长度为5的数据组。并使用队列读取前3个batch。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X0:  [[9970 9971 9972 9974 9975]\n",
      " [ 332 7147  328 1452 8595]\n",
      " [1969    0   98   89 2254]\n",
      " [   3    3    2   14   24]]\n",
      "Y0:  [[9971 9972 9974 9975 9976]\n",
      " [7147  328 1452 8595   59]\n",
      " [   0   98   89 2254    0]\n",
      " [   3    2   14   24  198]]\n",
      "X1:  [[9976 9980 9981 9982 9983]\n",
      " [  59 1569  105 2231    1]\n",
      " [   0  312 1641    4 1063]\n",
      " [ 198  150 2262   10    0]]\n",
      "Y1:  [[9980 9981 9982 9983 9984]\n",
      " [1569  105 2231    1  895]\n",
      " [ 312 1641    4 1063    8]\n",
      " [ 150 2262   10    0  507]]\n",
      "X2:  [[9984 9986 9987 9988 9989]\n",
      " [ 895    1 5574    4  618]\n",
      " [   8  713    0  264  820]\n",
      " [ 507   74 2619    0    1]]\n",
      "Y2:  [[9986 9987 9988 9989 9991]\n",
      " [   1 5574    4  618    2]\n",
      " [ 713    0  264  820    2]\n",
      " [  74 2619    0    1    8]]\n"
     ]
    }
   ],
   "source": [
    "# ptb_producer返回的为一个二维的tuple数据。\n",
    "result = reader.ptb_producer(train_data, 4, 5)\n",
    "\n",
    "# 通过队列依次读取batch。\n",
    "with tf.Session() as sess:\n",
    "    coord = tf.train.Coordinator()\n",
    "    threads = tf.train.start_queue_runners(sess=sess, coord=coord)\n",
    "    for i in range(3):\n",
    "        x, y = sess.run(result)\n",
    "        print \"X%d: \"%i, x\n",
    "        print \"Y%d: \"%i, y\n",
    "    coord.request_stop()\n",
    "    coord.join(threads)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
